package com.itawei.d1_reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 *获取构造方法！！！！
 *Class 类中用于获取构造方法的方法
 * Constructor[] getConstructors()
 * Constructor[] getDeclaredConstructors()
 * Constructor<T> getConstructor(Class<?>... parameterTypes)
 * Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
 *
 *
 * Constructor 类中用于创建对象的方法
 * T newInstance (Object... initargs)
 * setAccessible (boolean flag)
 *
 *
 *
 * */
public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1.获取Class对象
        Class clazz = Class.forName("com.itawei.d2_reflect.Teacher");
        //2.获取构造方法
        Constructor con1 = clazz.getConstructor();
        System.out.println(con1);
        System.out.println("------------------");

        Constructor[] cons2 = clazz.getConstructors();
        for (Constructor c : cons2) {
            System.out.println(c);
        }
        System.out.println("------------------");

        Constructor con3 = clazz.getDeclaredConstructor(int.class);
        System.out.println(con3);
        System.out.println("------------------");

        Constructor[] cons4 = clazz.getDeclaredConstructors();
        for (Constructor constructor : cons4) {
            System.out.println( constructor);
        }
        System.out.println("------------------");

        int m1 = con3.getModifiers();
        System.out.println(m1);
        System.out.println("------------------");
        int p1 = con3.getParameterCount();
        System.out.println(p1);
        System.out.println("------------------");


        //3.创建对象
        /*
           Constructor 类中用于创建对象的方法
         * T newInstance (Object... initargs)
         * setAccessible (boolean flag)
         */
        Constructor cc1 = clazz.getDeclaredConstructor(String.class, int.class);
        cc1.setAccessible(true);
        Teacher teacher = (Teacher) cc1.newInstance("左右", 18);
        System.out.println( teacher);
    }
}
